use std::{
    fs::OpenOptions,
    io::{self, Read, Seek, Write},
};

pub mod addtaost;
fn insert_into_file(path: &str, position: u64, content: &str) -> io::Result<()> {
    let mut file = OpenOptions::new().read(true).write(true).open(path)?;

    let mut contents = Vec::new();
    file.read_to_end(&mut contents)?;

    let mut new_contents = Vec::new();
    let mut start = Vec::new();
    let mut end = Vec::new();

    // Split the file contents at the insertion position
    new_contents.append(&mut contents[..position as usize].to_vec());
    start.append(&mut new_contents);
    end.append(&mut contents[position as usize..].to_vec());

    // Insert the new content
    start.extend_from_slice(content.as_bytes());
    start.extend_from_slice(&end);

    // Overwrite the file with the new contents
    file.set_len(start.len() as u64)?;
    file.seek(std::io::SeekFrom::Start(0))?;
    file.write_all(&start)?;
    file.flush()?;

    Ok(())
}
